﻿using BCL.BaseLib.Basics;
using DevicesView.Logic.App;
using DevicesView.Logic.Log;
using System;
using System.Linq;
using System.Windows;
using BCL.Tool;
using BCL.WPFComponent;
namespace DevicesView.Client
{
    static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            int id = System.Diagnostics.Process.GetCurrentProcess().Id;
            System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("DevicesViewClient");
            if (procs != null)
            {
                try
                {
                    System.Diagnostics.Process proc = procs.FirstOrDefault(o => o.Id != id);
                    if (proc != null)
                        proc.Kill();

                }
                catch (Exception)
                {
                }
            }

            BCLApplication.Resource = AppResource.LoadResource();
            BCLApplication.log = new BCLLog();
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-Hans");
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-Hans");
            BCLApplication.log.Write("", "1", BCL.Tool.OperationType.Start);

#if !DEBUG
            if (!Setup.Logic.Update())
                return;
#endif

            //加载预启动项
            MainApp app = new MainApp
            {
                ShutdownMode = ShutdownMode.OnExplicitShutdown
            };

//#if !DEBUG
            ////处理非UI线程异常
            //AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            //Application.Current.DispatcherUnhandledException += App_DispatcherUnhandledException;
            
//#endif

            
            
            app.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("/WPFComponent;component/Dictionary/GlobalDictionary.xaml", System.UriKind.Relative) });
            app.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("/WPFComponent;component/Dictionary/BorderStyle.xaml", System.UriKind.Relative) });
            app.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("/WPFComponent;component/Dictionary/DataGridDictionary.xaml", System.UriKind.Relative) });
            app.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("/WPFComponent;component/Dictionary/MuneDictionary.xaml", System.UriKind.Relative) });
            

            if (!FunctionLogic.StartAppLicationFunctions())
            {
                Application.Current.Shutdown();
                return;
            }

            BCL.WPFComponent.MainWindow window = new BCL.WPFComponent.MainWindow(FunctionLogic.GetFunctions());
            window.Closing += (send, e) => e.Cancel = UCMessageBox.Show("确定要退出程序嘛？", "退出", MessageBoxButton.OKCancel) != MessageBoxResult.OK;
            window.Closed += (sed, e) => Application.Current.Shutdown();
            
            Setup.InstallInfo info = Setup.Logic.DefaultInstallInfo();
            
            if (info != null)
            {
                //设置主窗口显示名称
                window.Title = info.DisplayName.Language();
                if (AppResource.CurrentResource.Hospital != null)
                    window.Title += "-"+AppResource.CurrentResource.Hospital.Name;
            }



            app.Run(window);
        }



        private static void App_Startup(object sender, StartupEventArgs e)
        {

        }








    }


    public class MainApp : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            DispatcherUnhandledException += App_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            base.OnStartup(e);
        }

        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            BCLApplication.log.Error(e.Exception);
            e.Handled = true;
        }
        #region 异常处理
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            BCLApplication.log.Error((Exception)e.ExceptionObject);
            
        }
        #endregion

    }

}
